import { CanvasRenderPage } from './render-page'
import { getPDFDocument } from './pdf-render-store'

class DocumentRender {
    constructor (src = '') {
        this.src = src // 文档资源

        this.pdfProxy = null // 文档PDFJS渲染对象
        this.firstPageProxy = null // 文档PDFJS第一页渲染对象
        this.numberOfPages = 0 // 文档页数

        this.pages = [] // 文档页面数组
        this.pagesMap = {} // 文档页面表
    }

    async init (src = this.src) {
        this.src = src

        const pages = []
        const pagesMap = {}
        const pdf = await getPDFDocument(src)
        const firstPage = await pdf.getPage(1)
        const numOfPages = pdf.numPages

        for (let i = 1; i <= numOfPages; i++) {
            const page = new CanvasRenderPage(i, pdf, firstPage)
            pages.push(page)
            pagesMap[i] = page
        }

        this.pdfProxy = pdf
        this.firstPageProxy = firstPage
        this.numOfPages = numOfPages
        this.pages = pages
        this.pagesMap = pagesMap

        return this
    }
}

export { DocumentRender }
